package com.youxin.logprocess;


import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.env.Environment;

import com.youxin.busfoundation.common.HandleShutdownApp;
import com.youxin.busfoundation.config.KafkaConfig;
import com.youxin.busfoundation.config.MonitorAlarmConfig;
import com.youxin.busfoundation.config.MonitorKafkaConfig;
import com.youxin.busfoundation.config.MonitorMysqlConfig;
import com.youxin.busfoundation.config.MonitorNginxVisitConfig;
import com.youxin.busfoundation.config.MonitorRedisConfig;
import com.youxin.busfoundation.config.MonitorWebConfig;
import com.youxin.busfoundation.config.MonitorZookeeperConfig;
import com.youxin.busfoundation.config.RedisClusterConfig;
import com.youxin.busfoundation.config.SparkConfig;
import com.youxin.busfoundation.config.ZookeeperConfig;
import com.youxin.foundation.db.redis.RedisClusterCommand;
import com.youxin.foundation.utils.thread.BaseThreadPool;
import com.youxin.foundation.utils.thread.ScheduleThreadPool;
import com.youxin.logprocess.controller.MediatorController;
import com.youxin.logprocess.controller.MonitorKafkaController;
import com.youxin.logprocess.controller.MonitorZookeeperController;
import com.youxin.logprocess.controller.SparkController;
import com.youxin.logprocess.controller.StatController;
import com.youxin.logprocess.controller.MonitorMySqlController;
import com.youxin.logprocess.controller.MonitorNginxController;
import com.youxin.logprocess.controller.MonitorRedisController;
import com.youxin.logprocess.controller.MonitorWebController;

/**
 * 
 * @author yingde.cao
 *
 */
@EnableConfigurationProperties({ RedisClusterConfig.class, KafkaConfig.class
	, SparkConfig.class,MonitorMysqlConfig.class,MonitorRedisConfig.class
	,MonitorWebConfig.class,MonitorZookeeperConfig.class
	,MonitorKafkaConfig.class,MonitorAlarmConfig.class
	,ZookeeperConfig.class,MonitorNginxVisitConfig.class})
@SpringBootApplication
public class LogProcessApplication {
		
//	@Value("${spring.datasource.primary.url}")
//	private String datasourceUrl;
	
	@Value("${spring.profiles.active}")
	private String active;
	
	private static Logger logger = Logger.getLogger(LogProcessApplication.class);
	@Autowired
	private RedisClusterConfig redisClusterConfig;	
	@Autowired
	private KafkaConfig kafkaConfig;	
	@Autowired
	private SparkConfig sparkConfig;	
	@Autowired
	private MonitorMysqlConfig monitorMysqlConfig;	
	@Autowired
	private MonitorRedisConfig monitorRedisConfig;	
	@Autowired
	private MonitorWebConfig monitorWebConfig;	
	@Autowired
	private MonitorZookeeperConfig monitorZookeeperConfig;	
	
	@Autowired
	private MonitorKafkaConfig monitorKafkaConfig;	
	
	@Autowired
	private MonitorAlarmConfig monitorAlarmConfig;	
	
	@Autowired
	private MonitorNginxVisitConfig monitorNginxConfig;
	
	/**
	 * zookeeper配置
	 */
	@Autowired
	private ZookeeperConfig zookeeperConfig;
	
	@Autowired
    Environment environment;
 
	/**
	 * 打印当前
	 */
    private void getActiveProfiles() {
        for (final String profileName : environment.getActiveProfiles()) {
            System.out.println("******当前使用的配置 - " + profileName);
        }   
    }
	
	public static void main(String[] args) {
		SpringApplicationBuilder builder = new SpringApplicationBuilder(LogProcessApplication.class)
			    .web(false);
		
//		Map<String, Object> defaultProperties = new HashMap<String, Object>();
//	    defaultProperties.put("property.one", "Value One");
//	    defaultProperties.put("property.two", "Value Two");
//	    
//	    builder.application().setDefaultProperties(defaultProperties);
		
		builder.run(args);		
		execute();
		
	}
	
	/**
	 * 执行的任务
	 */
	private static void execute()
	{
	    MediatorController.instance().execute();
	}	

	@PostConstruct
	private void init() {
		
		getActiveProfiles();
		try {
			HandleShutdownApp.instance().handleSignal(()->{
				SparkController.instance().stop();
			}
			);
		} catch (Exception e) {
			logger.error("加载HandleSignal异常", e);			
		}
		
		ScheduleThreadPool.instance(6);
		BaseThreadPool.instance(10,Integer.MAX_VALUE, 10000);
		try {			
			MonitorMySqlController.initConfig(monitorMysqlConfig);
		} catch (Exception e) {
			logger.error("加载Monitor Mysql Config异常", e);			
		}
		
		try {			
			MonitorWebController.initConfig(monitorWebConfig);
		} catch (Exception e) {
			logger.error("加载Monitor Web Config异常", e);			
		}
		
		try {			
			MonitorKafkaController.initConfig(monitorKafkaConfig);
		} catch (Exception e) {
			logger.error("加载Monitor Kafka Config异常", e);			
		}
		
		try {			
			MonitorZookeeperController.initConfig(monitorZookeeperConfig);
		} catch (Exception e) {
			logger.error("加载Monitor Zookeeper Config异常", e);			
		}
		
		try {			
			MonitorRedisController.initConfig(monitorRedisConfig);
		} catch (Exception e) {
			logger.error("加载Monitor Redis Config异常", e);			
		}
		
		try {			
			StatController.initConfig(monitorAlarmConfig.getAlarm().getMaxtimes());
			MonitorNginxController.initConfig(monitorNginxConfig);
			
			MonitorAlarmConfig.initConfig(monitorAlarmConfig);
			KafkaConfig.initConfig(kafkaConfig);
			SparkConfig.initConfig(sparkConfig);
			RedisClusterCommand.initConfig(redisClusterConfig);

		} catch (Exception e) {
			logger.error("加载异常", e);			

		}
			
		
	}
}
